﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agresso.Interface.Layer.Gateway;
using System.Collections;
using System.Data.SqlClient;
using Agresso.Interface.Layer.SQL;
using Agresso.Interface.Layer.Domain;

namespace Agresso.Interface.Layer.Service
{
    public class c_GRN_Service
    {
        DataBaseConnectivity m_dataBaseConectivity;
        public c_GRN_Service()
        {
            m_dataBaseConectivity = new DataBaseConnectivity();
        }
        public IList GetAllPendingGRNArrivedID()
        {
            IList result = new ArrayList();
            m_dataBaseConectivity.AgrTempConnectionSQLConnection.Open();
            SqlCommand cmd = new SqlCommand(AgressoSQL.GetAllPendingGRNArrivedID(), m_dataBaseConectivity.AgrTempConnectionSQLConnection);
            SqlDataReader rd = cmd.ExecuteReader();
            while(rd.Read())
            {
                result.Add(Convert.ToInt64(rd["arrive_id"]));
            }
            m_dataBaseConectivity.AgrTempConnectionSQLConnection.Close();
            return result;
        }

        internal IList GetAllGRNByArrivedID(long arrived_id)
        {
            IList result = new ArrayList();
            m_dataBaseConectivity.AgrTempConnectionSQLConnection.Open();
            SqlCommand cmd = new SqlCommand(AgressoSQL.GetAllGRNByArrivedID(), m_dataBaseConectivity.AgrTempConnectionSQLConnection);
            cmd.Parameters.Add("@arrive_id", System.Data.SqlDbType.BigInt).Value = arrived_id;
            SqlDataReader rd = cmd.ExecuteReader();
            while (rd.Read())
            {
                result.Add(new c_GRN(rd));
            }
            m_dataBaseConectivity.AgrTempConnectionSQLConnection.Close();
            return result;
        }

        internal void UpdatePickedUpFlagResultByArriveID(long arrivedID, int status, string result)
        {
            m_dataBaseConectivity.AgrTempConnectionSQLConnection.Open();
            SqlCommand cmd = new SqlCommand(AgressoSQL.UpdatePickedUpFlagResultByArriveID(), m_dataBaseConectivity.AgrTempConnectionSQLConnection);
            cmd.Parameters.Add("@PickedUpFlag", System.Data.SqlDbType.Int).Value = status;
            cmd.Parameters.Add("@ResultDetails", System.Data.SqlDbType.NVarChar).Value = result;
            cmd.Parameters.Add("@arrive_id", System.Data.SqlDbType.BigInt).Value = arrivedID;
            cmd.ExecuteNonQuery();
            m_dataBaseConectivity.AgrTempConnectionSQLConnection.Close();
        }
    }
}
